#include <qqtdataconverter.h>

#include <QTextCodec>

QQtDataConverter::QQtDataConverter ( QObject* parent )
    : QObject ( parent ) {}

QQtDataConverter::~QQtDataConverter() {}

void QQtDataConverter::fromGBK ( const QByteArray& bytes )
{
    QTextCodec* codec = QTextCodec::codecForName ( "GBK" );
    if ( !codec )
        return;
    mBytes = codec->toUnicode ( bytes ).toLocal8Bit();
}

QByteArray QQtDataConverter::toGBK()
{
    QTextCodec* codec = QTextCodec::codecForName ( "GBK" );
    if ( !codec )
        return QByteArray();
    return codec->fromUnicode ( QString ( mBytes ) );
}

void QQtDataConverter::fromUTF8 ( const QByteArray& bytes )
{
    QTextCodec* codec = QTextCodec::codecForName ( "UTF-8" );
    if ( !codec )
        return;
    mBytes = codec->toUnicode ( bytes ).toLocal8Bit();
}

QByteArray QQtDataConverter::toUTF8()
{
    QTextCodec* codec = QTextCodec::codecForName ( "UTF-8" );
    if ( !codec )
        return QByteArray();
    return codec->fromUnicode ( QString ( mBytes ) );
}

void QQtDataConverter::fromEncoding ( const QByteArray& bytes, QByteArray encoding )
{
    QTextCodec* codec = QTextCodec::codecForName ( encoding );
    if ( !codec )
        return;
    mBytes = codec->toUnicode ( bytes ).toLocal8Bit();
}

QByteArray QQtDataConverter::toEncoding ( QByteArray encoding )
{
    QTextCodec* codec = QTextCodec::codecForName ( encoding );
    if ( !codec )
        return QByteArray();
    return codec->fromUnicode ( QString ( mBytes ) );
}
